package org.springblade.task.job.report.user;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.userAnalysis.entity.DailyNewUserReport;
import org.springblade.modules.userAnalysis.service.IDailyNewUserReportService;
import org.springblade.task.base.entity.BaseUser;
import org.springblade.task.base.param.BaseUserParam;
import org.springblade.task.base.service.*;
import org.springblade.task.base.vo.BaseUserVO;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: 用户分析 > 新增用户
 * @author syncwt
 * @version V1.0
 */
@Component
public class DailyNewUserReportJob {
	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	@Autowired
	private IBaseUserService baseUserService;
	@Autowired
	private IBaseVipUserService baseVipUserService;
	@Autowired
	private IBaseUserVisitLogService baseUserVisitLogService;
	@Autowired
	private IDailyNewUserReportService dailyNewUserReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("dailyNewUserReportJob")
	public ReturnT<String> dailyNewUserReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailyNewUserReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailyNewUserReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		DailyNewUserReport report = new DailyNewUserReport();
		int vipNum = 0;
		int stationUserNum = 0;
		int outsideUserNum = 0;
			// 查询首次付费用户
		List<String> payUserList = baseUserService.queryFirstPaymentUserList(taskDate);
		int newPaidUsersNum = payUserList == null ? 0: payUserList.size();
		report.setNewPaidUsersNum(newPaidUsersNum);

		BaseUserParam param = new BaseUserParam();
		param.setStartDate(DateUtils.getDateStartTime(taskDate));
		param.setEndDate(DateUtils.getDayEndTime(taskDate));
		// 新增登陆用户：今日首次登陆的用户
		List<BaseUserVO> userList = baseUserService.queryTodayLoginUserList(param);
		List<String> pinList = new ArrayList<>();
		if(!CollectionUtils.isEmpty(userList)) {
			for (BaseUserVO user: userList) {
				pinList.add(user.getPin());
			}

			BaseUserParam vipParam = new BaseUserParam();
			vipParam.setPinList(pinList);
			vipParam.setStartDate(DateUtils.getDateStartTime(taskDate));
			vipParam.setEndDate(DateUtils.getDayEndTime(taskDate));
			// 新增vip用户数 ，查询今日首次登陆用户，并且登陆日期在VIP有效区间内
			vipNum = baseVipUserService.queryByPinListCount(vipParam);
			// 站内用户
			BaseUserParam userVisitParam = new BaseUserParam();
			userVisitParam.setPinList(pinList);
			userVisitParam.setStartDate(DateUtils.getDateStartTime(taskDate));
			userVisitParam.setEndDate(DateUtils.getDayEndTime(taskDate));
			stationUserNum = baseUserVisitLogService.queryByTodayStationUserList(userVisitParam);

			// 站外用户
			outsideUserNum = userList.size() - stationUserNum;


		}

		report.setDate(taskDate);
		report.setNewVipUsersNum(vipNum);
		report.setNewUsersNum(userList == null ? 0:userList.size());
		report.setNewInUsersNum(stationUserNum);
		report.setNewOutUsersNum(outsideUserNum);
		dailyNewUserReportService.save(report);

		return;
	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
